package com.wiscamp.ninechapters.problems.adapter.resources.reports;

import com.wiscamp.ninechapters.problems.application.dto.commands.reports.CreateTaskByReportCommand;
import com.wiscamp.ninechapters.problems.application.dto.vo.problems.ProblemKnowledgePointVO;
import com.wiscamp.ninechapters.problems.application.dto.vo.reports.TaskReportVO;
import com.wiscamp.ninechapters.problems.application.services.TaskReportAppService;
import com.wiscamp.ninechapters.problems.domain.models.challenges.AmcSummaryReport;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 答题卡服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/Problems")
@Tag(name = "题库-答题卡信息")
public class TaskReportController extends BaseController {

    private final TaskReportAppService taskReportAppService;

    public TaskReportController(TaskReportAppService taskReportAppService) {
        this.taskReportAppService = taskReportAppService;
    }


    @GetMapping("Tasks/{taskId}/Report/Incorrect/KnowledgePoints")
    @Operation(summary = "通过练习任务编号获取报告中错题关联知识点列表")
    @HandleResourceException
    public RestResponse<Collection<ProblemKnowledgePointVO>> getPointsByIncorrectProblemsInReport(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                                                  @PathVariable("taskId") long taskId) {
        var result = taskReportAppService.getPointsByIncorrectProblemsInReport(taskId);
        return ResourceUtils.getOkResponse(request, response, result);
    }

    /**
     * 通过练习任务编号获取报告中错题数量
     *
     * @param request
     * @param response
     * @param taskId
     * @return
     */
    @GetMapping("Tasks/{taskId}/Report/Incorrect/Problems/Quantity")
    @Operation(summary = "通过练习任务编号获取报告中错题数量")
    @HandleResourceException
    public RestResponse<Integer> getIncorrectProblemQuantity(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                             @PathVariable("taskId") long taskId) {
        var result = taskReportAppService.getIncorrectProblemQuantity(taskId);
        return ResourceUtils.getOkResponse(request, response, result);
    }

    /**
     * 通过任务编号获取任务报告
     *
     * @param request
     * @param response
     * @param taskId
     * @return
     */
    @GetMapping("Tasks/{taskId}/Report")
    @Operation(summary = "通过任务编号获取任务报告")
    @HandleResourceException
    public RestResponse<TaskReportVO> getReportByTask(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                      @PathVariable("taskId") long taskId) {
        var result = taskReportAppService.getReportByTask(taskId);
        return ResourceUtils.getOkResponse(request, response, result);
    }

    @GetMapping("Tasks/{taskId}/Report/AMC")
    @Operation(summary = "通过用户编号及任务编号获取AMC模考报告")
    @HandleResourceException
    public RestResponse<AmcSummaryReport> getAmcSummaryReport(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                              @PathVariable("taskId") long taskId) {
        var result = taskReportAppService.analyseAmcScore(taskId);
        return ResourceUtils.getOkResponse(request, response, result);
    }

    /**
     * 通过任务编号获取错题知识点关联的新题并创建新练习任务
     *
     * @param request
     * @param response
     * @param taskId
     * @return
     */
    @GetMapping("Tasks/{taskId}/Report/Actions/Generate/Task/{problemQuantity}")
    @Operation(summary = "通过任务编号获取错题并创建关联新任务")
    @HandleResourceException
    public RestResponse<Boolean> createTaskByReport(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                    @RequestBody CreateTaskByReportCommand command) {
        if (command.getProblemQuantity() > 10) command.setProblemQuantity(10);
        var result = taskReportAppService.createTaskByReport(command);
        return ResourceUtils.getOkResponse(request, response, result);
    }
}
